package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.CalenderFormModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zyh on 2018/5/18.
 */

public class CalendarAdapter extends RecyclerView.Adapter<CalendarAdapter.ViewHolder> {

    private Context context;

    private LayoutInflater layoutInflater;

    private List<CalenderFormModel.Rls> list;

    public CalendarAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        list = new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.item_calendar_form, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        CalenderFormModel.Rls data = list.get(position);
        if (data != null) {
            if (!TextUtils.isEmpty(data.getBskssj()) && !TextUtils.isEmpty(data.getBsjssj())) {
                String start = data.getBskssj().substring(5, data.getBskssj().length());
                String end = data.getBsjssj().substring(5, data.getBsjssj().length());
                if (position == list.size() - 1)
                    holder.textView1_layout.setBackgroundResource(R.drawable.shape_calender_form_item_left_last);
                else
                    holder.textView1_layout.setBackgroundResource(R.drawable.shape_calender_form_item_left);
                holder.textView1.setCompoundDrawablesWithIntrinsicBounds(data.getColor(), 0, 0, 0);
                holder.textView1.setText(String.valueOf(removeZero(start) + "-" + removeZero(end)));
            } else {
                if (position == list.size() - 1) {
                    holder.textView1_layout.setBackgroundResource(R.drawable.shape_calender_form_item_left_null_last);
                } else
                    holder.textView1_layout.setBackgroundResource(R.drawable.shape_calender_form_item_left_null);
                holder.textView1.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                holder.textView1.setText("");
            }
            if (position == list.size() - 1) {
                holder.textView2.setBackgroundResource(R.drawable.shape_calender_form_item_last);
                holder.textView3.setBackgroundResource(R.drawable.shape_calender_form_item_right_last);
            } else {
                holder.textView2.setBackgroundResource(R.drawable.shape_calender_form_item);
                holder.textView3.setBackgroundResource(R.drawable.shape_calender_form_item_right);
            }
            holder.textView2.setText(data.getBssz());
            holder.textView3.setText(data.getBz());
        }
    }

    private String removeZero(String str) {
        int m = Integer.valueOf(str.substring(0, 2));
        int e = Integer.valueOf(str.substring(3, str.length()));
        return m + "." + e;
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setList(List<CalenderFormModel.Rls> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private TextView textView1, textView2, textView3;
        private View textView1_layout;

        public ViewHolder(View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.text1);
            textView2 = itemView.findViewById(R.id.text2);
            textView3 = itemView.findViewById(R.id.text3);
            textView1_layout = itemView.findViewById(R.id.text1_layout);
        }

    }

    public void clear() {
        if (list != null) {
            list.clear();
            list = null;
        }
    }
}
